package com.lib.framework.widget.dialog.bottomlist

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.lib.framework.R
import com.lib.framework.databinding.ItemBottomListTitleBinding
import com.lib.framework.databinding.WidgetBottomListDialogBinding
import com.lib.framework.widget.dialog.BaseDialog
import com.lib.framework.widget.WidgetUtil

/**
 * 底部列表对话框
 *
 * @author keanbin
 */
class BottomListDialog(context: Context, itemList: MutableList<BottomListDialogItem>) : BaseDialog(context, R.style.defaultDialogTheme04) {

    private var mItemList = itemList

    private var mDataBinding: WidgetBottomListDialogBinding? = null

    private var mOnItemClickListener: OnItemClickListener? = null

    /**
     * 分割线的颜色
     */
    private var mSeparatorColor: Int = 0xffECECEC.toInt()

    /**
     * 分隔线的高度
     */
    private var mSeparatorHeight = 1

    /**
     * 多item时，中间分割线左边Margin
     */
    private var mMiddleSeparatorLeftMargin = 0

    /**
     * 多item时，中间分割线右边Margin
     */
    private var mMiddleSeparatorRightMargin = 0

    /**
     * 默认操作文本颜色
     */
    private var mDefaultTextColor: Int = 0xff333333.toInt()

    /**
     * 默认标题文本颜色
     */
    private var mDefaultTitleTextColor: Int = mContext.resources.getColor(R.color.grayDefaut)

    /**
     * 是否有列表分割线
     */
    var isHasSeparator = false


    init {
        initView()
        window?.let {
            it.setGravity(Gravity.BOTTOM) // 此处可以设置dialog显示的位置
            it.setWindowAnimations(R.style.BottomInAndOutStyle) // 添加动画

            // 设置 宽度充满整个屏幕
            val display = it.windowManager.defaultDisplay
            it.attributes.width = display.width //设置dialog的宽度为当前手机屏幕的宽度
        }

    }

    /**
     * 初始化视图
     */
    private fun initView() {
        mDataBinding = DataBindingUtil.inflate(layoutInflater, R.layout.widget_bottom_list_dialog, null, false)
        mDataBinding?.let { dataBinding ->
            setContentView(dataBinding.root)

            fillView()
        }
    }

    /**
     * 通知数据发生改变
     */
    fun notifyDataChange() {
        fillView()
    }

    fun setTextColor(textColor: Int) {
        this.mDefaultTextColor = textColor
        notifyDataChange()
    }

    /**
     * 填充视图
     */
    private fun fillView() {
        mDataBinding?.let { dataBinding ->
            dataBinding.llList.removeAllViews()

            for (i in 0 until mItemList.size) {
                if (isHasSeparator && i > 0) {
                    // 添加 分隔线
                    addSeparatorView(dataBinding.llList, mSeparatorHeight, mSeparatorColor, 0, 0)
                }

                // 添加文本项
                addItemView(dataBinding.llList, mItemList[i], i)
            }
        }
    }

    /**
     * 加入 ItemView
     */
    private fun addItemView(viewGroup: ViewGroup, item: BottomListDialogItem, position: Int) {
        when (item.flag) {
            BottomListDialogItem.FLAG_TITLE -> {
                addTitleItemView(viewGroup, item, position)
            }

            BottomListDialogItem.FLAG_OPERATE -> {
                addOperateItemView(viewGroup, item, position)
            }
        }
    }

    /**
     * 加入 操作 ItemView
     */
    private fun addOperateItemView(viewGroup: ViewGroup, item: BottomListDialogItem, position: Int) {
        val textView = TextView(mContext)
        val textColor = item.textColor ?: mDefaultTextColor
        textView.setTextColor(textColor)
        textView.textSize = 17.0f
        textView.gravity = Gravity.CENTER
        textView.text = item.text

        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                WidgetUtil.dpTopx(mContext, 50))
        lp.setMargins(0, 0, 0, 0)
        textView.layoutParams = lp
        viewGroup.addView(textView)

        textView.setOnClickListener {
            mOnItemClickListener?.onItemClick(it, item.id, position)
        }
    }

    /**
     * 加入 标题 ItemView
     */
    private fun addTitleItemView(viewGroup: ViewGroup, item: BottomListDialogItem, position: Int) {
        val binding = DataBindingUtil.inflate<ItemBottomListTitleBinding>(LayoutInflater.from(this.mContext),
                R.layout.item_bottom_list_title, viewGroup, false)

        binding.tvTitle.text = item.text
        val textColor = item.textColor ?: mDefaultTitleTextColor
        binding.tvTitle.setTextColor(textColor)

        val titleContent = item.titleContent
        if (!titleContent.isNullOrEmpty()) {
            binding.tvTitleContent.text = item.titleContent
            binding.tvTitleContent.visibility = View.VISIBLE
        } else {
            binding.tvTitleContent.visibility = View.GONE
        }

        viewGroup.addView(binding.root)
    }

    /**
     * 动态增加一项
     */
    fun dynamicAddItem(item: BottomListDialogItem) {
        mItemList.add(item)
        notifyDataChange()
    }

    /**
     * 动态增加一项
     */
    fun dynamicAddItem(index: Int, item: BottomListDialogItem) {
        mItemList.add(index, item)
        notifyDataChange()
    }


    /**
     * 加入分割线
     *
     * @param groupListView
     * @param height
     * @param separatorColor
     * @param leftMargin
     * @param rightMargin
     */
    private fun addSeparatorView(viewGroup: ViewGroup, height: Int, separatorColor: Int, leftMargin: Int, rightMargin: Int) {
        val view = View(mContext)
        view.setBackgroundColor(separatorColor)
        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height)
        lp.setMargins(leftMargin, 0, rightMargin, 0)
        view.layoutParams = lp
        viewGroup.addView(view)
    }

    /**
     * 设置监听器
     */
    fun setOnItemClickListener(l: OnItemClickListener) {
        mOnItemClickListener = l
    }

    /**
     * 设置取消按钮的点击监听器
     */
    fun setCancelOnClickListener(l: View.OnClickListener) {
        mDataBinding?.cancle?.setOnClickListener(l)
    }

    /**
     * 设置取消按钮文本
     */
    fun setCancelText(text: String) {
        mDataBinding?.cancle?.text = text
    }

    /**
     * 点击监听器
     */
    interface OnItemClickListener {

        /**
         * 点击回调
         */
        fun onItemClick(view: View, id:Int, position: Int)
    }
}